package com.example.user.controller;


import com.example.common.core.constants.HttpConstants;
import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.R;
import com.example.common.core.domain.vo.LoginUserVO;
import com.example.user.domain.student.dto.StudentDTO;
import com.example.user.domain.student.dto.StudentSaveDTO;
import com.example.user.domain.student.vo.StudentDetailVO;
import com.example.user.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController extends BaseController {

    @Autowired
    private IStudentService studentService;

    // 获取验证码
    @PostMapping("sendCode")
    public R<Void> sendCode(@RequestBody @Validated StudentDTO studentDTO) {
        return toR(studentService.sendCode(studentDTO));
    }

    // 用户登录
    @PostMapping("/code/login")
    public R<String> login(@RequestBody @Validated StudentDTO studentDTO) {
        String token = studentService.codeLogin(studentDTO.getEmail(),studentDTO.getCode());
        return R.ok(token);
    }

    // 退出登录
    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return toR(studentService.logout(token));
    }

    @GetMapping("/info")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return studentService.info(token);
    }

    @GetMapping("/detail")
    public R<StudentDetailVO> detail(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return studentService.detail(token);
    }

    @PutMapping("/edit")
    public R<Void> edit(@RequestBody StudentSaveDTO studentSaveDTO) {
        return toR(studentService.edit(studentSaveDTO));
    }

    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody StudentSaveDTO studentSaveDTO) {
        return toR(studentService.updateHeadImage(studentSaveDTO.getHeadImage()));
    }

}
